# QYTTS
#
QsYouTubeTrailerScraper - QYTTS v0.25

This script will scrape your Kodi library for movie titles, then search
YouTube for it's trailer. It'll download the trailers to a specified
directory, which is used by the Cinema Experience script for Kodi to
load trailers. This way you can see the trailers for films you own, 
withought having to deal with all the buffering that comes from streaming.

I know there are other tools out there to get trailers for your library,
but most were windows based. The ones that weren't would just grab trailers
for any film, whether you have it or not.

This was designed for my original Raspberry Pi Model B, running OSMC.
As such, it'll probably run on any Linux Kodi setup.


---------
Changelog
---------
- v0.25 - formatiing clean-up
- v0.24 - log more youtube-dl errors correctly
- v0.23 - create trailer directory if it doesn't exist, to avoid copy errors
- v0.22 - detect current kodi database, minimal config now required
- v0.21 - cleaning up formating, bugfix for videoId's that start with special characters
- v0.20 - fixed bug with temp cleanup
- v0.19 - improved comments, and failure logging clarified and saved to disk
- v0.18 - more error handling, updated trailers from .avi to .mp4 extensions
- **RENAME OLD FILES USING BULK RENAMER TOOL TO AVOID DUPLICATES**
- v0.17 - provide https links to youtube-dl
- v0.16 - added clean-up code for youtube-dl errors
- v0.15 - changed trailer naming scheme for better human readability
- **DELETE OLD TRAILERS TO AVOID DUPES, NEW NAMING SCHEME WILL HAVE TO REDOWNLOAD ALL**
- v0.14 - scraper reworked for current YT homepage
- v0.13 - added option to fix missing dependencies
- v0.12 - various tweaks to fix some neglect
- v0.11 - migrated away from GitHub because MICROS~1
- v0.10 - filename handling fixed this time, honest
- v0.9 - youtubedl filename handling corrected
- v0.8 - scraper udated - again - YT needs to stop changing
- v0.7 - fixed scraper, YT changes broke everything
- v0.6 - updated base config for new version of Kodi database
- v0.5 - produce list of failed film at end of script - run alphabetically
- v0.4 - added quick dependency check
- v0.3 - added better trailer detection
- v0.2 - complete reworking of proof of concept version
- v0.1 - proof of concept


----
Usage
-----
This does work "out of the box" now, but the defaults may not be ideal.

There is one main configuration variable to change, the location to store downloaded trailers. This currently defaults to "~/Trailers/".

There are some other options that can be tweaked in addition:-
- Kodi database directory (if you have a non-standard install)
- Temporary files directory - should you need to use something other than /tmp/ (only really needed if debugging this script)
- Max size for trailers (expressed in bytes)
- Fix dependencies automatically (off by default)


------------
Dependencies
------------
bash shell environment

SQLite3
- install with "sudo apt-get install sqlite3"

youtube-dl
- install with "sudo curl https://yt-dl.org/latest/youtube-dl -o /usr/local/bin/youtube-dl ; sudo chmod a+rx /usr/local/bin/youtube-dl"

You can set a config option in the script to automatically try and fix dependencies,
currently this is only for systems using APT as a package manager. This is off by
default to avoid making unwanted changes to your system.


------------
Known Issues
------------
youtube-dl errors

It is *IMPERATIVE* that you use an up-to-date version of youtube-dl, as they
are playing whack-a-mole with YouTube servers constantly. I have found that
the version kept in some repos (namely raspbian) isn't always up to date. I
highly recommend installing it direct from the developers as mentioned above.
If you do run into any issues, running "youtube-dl -U" to update can often
fix problems grabbing trailers.

Grabbing entire films

Due to the simplicity of this script, it will grab the first YouTube result
after searching for the movie name + "trailer". In the case of some films - 
mostly older or independent films - the first result on youtube will be
the complete film. To avoid this issue, there are 2 countermeasures - it
will check the title of the YouTube video to ensure it contains the word
"Trailer" (case insensitive). Secondly, after downloading it checks if it
is larger than the maximum file size (default is 50MB), and if it is too 
large then deletes the file. Unfortunately I have no way to check file size
before downloading, so looking at extra ways of avoiding unnecessary 
bandwidth usage...

YT updating their site

YouTube seem to be updating their search result page with increasing
frequency, breaking my dirty-hack scraper. I currently have a good setup,
which may last for some time. But now I know youtube-dl can also do search
I will switch my method if Google break this script again.
 
If you encounter any other issues, please raise an issue on notabug or email
(https://notabug.org/Category/QYTTS)
 


 
Copyright © 2015 Category <categoryNOSPAM@quintendo.uk>
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See the COPYING file for more details.
